﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;

namespace ChinaSoft.BLL.Common
{
    /// <summary>
    /// 树形控件操作类
    /// </summary>
    public class TreeViewOperate
    {
        /// <summary>
        /// 绑定TreeView控件
        /// 作者：demo
        /// 时间：2012-4-10
        /// </summary>
        /// <param name="ds">数据源</param>
        /// <param name="treeview">TreeView控件</param>
        /// <param name="filedText">前台显示的字段名称</param>
        /// <param name="filedValue">后台绑定的字段名称</param>
        /// <param name="father">区分父子级关系的字段名称</param>
        ///  <param name="Expanded">是否展开节点</param>
        public void BindTreeView(DataSet ds,TreeView treeview,string filedText,string filedValue,string father,bool Expanded)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataView dv = ds.Tables[0].DefaultView;
                dv.RowFilter = father + "=0 or "+father+" is null";
                foreach (DataRowView drv in dv)
                {
                    TreeNode node = new TreeNode();
                    node.Text = drv[filedText].ToString();
                    node.Value = drv[filedValue].ToString();
                    node.Expanded = Expanded;

                    treeview.Nodes.Add(node);
                    AddNodes(dv, node, filedText, filedValue, father, Expanded);
                }
            }
        }
        /// <summary>
        /// 绑定单层树形菜单（无父级）
        /// 作者：demo
        /// 时间：2012-4-12
        /// </summary>
        /// <param name="ds">数据源</param>
        /// <param name="treeview">TreeView控件</param>
        /// <param name="filedText">前台显示的字段名称</param>
        /// <param name="filedValue">后台绑定的字段名称</param>
        /// <param name="Expanded">是否展开节点</param>
        public void BindTreeView(DataSet ds, TreeView treeview, string filedText, string filedValue, bool Expanded)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataView dv = ds.Tables[0].DefaultView;
                dv.RowFilter = " ";
                foreach (DataRowView drv in dv)
                {
                    TreeNode node = new TreeNode();
                    node.Text = drv[filedText].ToString();
                    node.Value = drv[filedValue].ToString();
                    node.Expanded = Expanded;
                    treeview.Nodes.Add(node);
                    
                }
                int i=treeview.Nodes.Count;
            }
        }
        /// <summary>
        /// 通过递归添加节点
        /// 作者:demo
        /// 时间:2012-4-10
        /// </summary>
        /// <param name="dv">数据视图</param>
        /// <param name="node">节点</param>
        /// <param name="filedText">前台显示的字段</param>
        /// <param name="filedValue">后台绑定的值</param>
        /// <param name="father">区分父子级关系的字段名称</param>
        /// <param name="Expanded">控制节点是否关闭</param>
        public void AddNodes(DataView dv, TreeNode node, string filedText, string filedValue, string father, bool Expanded)
        {
            dv.RowFilter = father+"='" + node.Value + "'";
            foreach (DataRowView row in dv)
            {
                TreeNode replyNode = new TreeNode();
                replyNode.Text = row[filedText].ToString();
                replyNode.Value = row[filedValue].ToString();
                replyNode.Expanded = Expanded;
                node.ChildNodes.Add(replyNode);
                AddNodes(dv, replyNode,filedText,filedValue,father,Expanded);
            }
        }
    }
}
